package com.kgc.scd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kgc.scd.bean.User;
import com.kgc.scd.bean.UserVO;
import com.kgc.scd.mapper.UserMapper;
import com.kgc.scd.service.UserService;
import com.kgc.scd.util.MD5Util;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author: WangKuoKuo
 * @create: 2022-06-09 19:50
 * @description:
 **/
@Slf4j
@RestController
// @Api(tags = "用户个人中心")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation("修改用户密码")
    @PostMapping("/modUserPwd")
    public RequestResult<String> modUserPwd(@RequestParam String userId,
                                          @RequestParam(required = false) String pwd,
                                          @RequestParam String newPwd,
                                          @RequestParam String newAgainPwd) {
        //从数据库中查询user
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        User userFromDB = userService.getOne(queryWrapper);

        // 做非空判断  传过来是空密码，数据库中还是空密码
        if ((ObjectUtils.isEmpty(pwd)) && null == userFromDB.getUserPwd()) {
            // 两次新密码是否输入一致  如果一致
            return getUserRequestResult(userId, newPwd, newAgainPwd, queryWrapper);
        }

        // 数据库中的原密码和用户传过来的密码旧密码作验证
        if ((userId.equals(userFromDB.getUserId()) &&
                MD5Util.md5Password(pwd).equals(userFromDB.getUserPwd()))) {
            log.info("数据库中的原密码和用户传过来的密码旧密码验证通过");

            // 两次新密码是是否一致
            return getUserRequestResult(userId, newPwd, newAgainPwd, queryWrapper);

        }

        return ResultBuildUtil.fail("501", "原密码有误！");

    }

    /**
     * @param : [userId, newPwd, newAgainPwd, queryWrapper]
     * @return : com.kgc.scd.util.RequestResult<com.kgc.scd.bean.User>
     * @author : WangKuoKuo
     * @date : 2022/6/15/015
     * @description : 两次新密码是是否一致
     */
    private RequestResult getUserRequestResult(@RequestParam String userId,
                                                     @RequestParam String newPwd,
                                                     @RequestParam String newAgainPwd,
                                                     QueryWrapper<User> queryWrapper) {
        if (newPwd.equals(newAgainPwd)) {

            // 给传过来要修改的密码加密
            String md5Password = MD5Util.md5Password(newPwd);
            // 更新密码
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("user_pwd", md5Password)
                    .eq("user_id", userId);
            userService.update(updateWrapper);
            log.info("用户:{},密码修改成功！", userId);
            // 再次查询用户信息 根据id查询用户信息
            //QueryWrapper<User> queryWrapperAgain = new QueryWrapper<>();
            //queryWrapperAgain.eq("user_id", userId);
            //User user = userService.getOne(queryWrapper);
            //log.info("用户:{}", user);
            return ResultBuildUtil.success("密码修改成功");

        }
        log.info("修改失败，两次密码不一致！");
        return ResultBuildUtil.fail("502", "密码修改失败，两次密码输出不一致！");
    }



    @ApiOperation("查询个人中心")
    @PostMapping("/queryPersonCenterInfo")
    @ApiImplicitParam(name = "userId", value = "根据用户ID查询")
    public RequestResult<UserVO> queryPersonCenterInfo(@RequestParam String userId) {


        // 查询数用户信息
        UserVO userVO = userService.queryUserInfo(userId);
        log.info("用户信息查询为：{}", userVO);

        return ResultBuildUtil.success(userVO);

    }

    //@ApiOperation("修改个人中心")
    //@PostMapping("/modifyPersonCenterInfo")
    //public RequestResult<String> modifyPersonCenterInfo(@RequestParam String userId,
    //                                                  @RequestParam(value = "userName", required = false) String userName,
    //                                                  @RequestParam(value = "userPhone", required = false) String userPhone,
    //                                                  @RequestParam(value = "userBirth", required = false) Date userBirth,
    //                                                  @RequestParam(value = "userEmail", required = false) String userEmail,
    //                                                  @RequestParam(value = "userSex", required = false) String userSex,
    //                                                  @RequestParam(value = "userAddrId", required = false) String userAddrId) {
    //    UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
    //
    //    userUpdateWrapper.set(userName != null, "user_name", userName)
    //            .set(userPhone != null, "user_phone", userPhone)
    //            .set(userBirth != null, "user_birth", userBirth)
    //            .set(userEmail != null, "user_email", userEmail)
    //            .set(userSex != null, "user_sex", userSex)
    //            .set(userAddrId != null, "user_addr_id", userAddrId)
    //            .eq("user_id", userId);
    //
    //    boolean update = userService.update(userUpdateWrapper);
    //    //QueryWrapper<User> wrapper = new QueryWrapper<>();
    //    //wrapper.eq("user_id",userId);
    //    //User userServiceById = userService.getOne(wrapper);
    //    //log.info("个人中心修改数据完成：用户名:{},手机号:{},生日:{},个人邮箱:{},性别:{},默认地址：{}"
    //    //        , userName, userPhone, userBirth, userEmail, userSex, userAddrId);
    //    if (update){
    //        return ResultBuildUtil.success("修改成功");
    //    }else{
    //        return ResultBuildUtil.fail("500","修改失败");
    //    }
    //}


    @ApiOperation("修改个人中心")
    @PostMapping("/modifyPersonCenterInfo")
    public RequestResult<String> modifyPersonCenterInfo(@RequestParam("userId") String userId,
                                                        @RequestBody User user) {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("user_id",userId);
        int update = userMapper.update(user, userUpdateWrapper);
        if (update > 0){
            return ResultBuildUtil.success("修改成功");
        }else{
            return ResultBuildUtil.fail("500","修改失败");
        }
    }



}
